package com.example.analysis.service.impl;

import com.example.analysis.entity.Course;
import com.example.analysis.entity.ElectionList;
import com.example.analysis.entity.Teacher;
import com.example.analysis.entity.TeacherCourseClass;
import com.example.analysis.mapper.ElectionListMapper;
import com.example.analysis.service.ElectionListService;
import com.example.analysis.utils.DifferenceAnalysis;
import com.example.analysis.utils.NormalityTest;
import com.example.analysis.vo.ItemVO;
import com.example.analysis.vo.ResultVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class ElectionListServiceImpl implements ElectionListService {
    @Resource
    private ElectionListMapper mapper;

    @Override
    public void insert() {

        List<ElectionList> list = new ArrayList<>();
        int id = 61;
        String pre = "s";
        Random random = new Random();
        for (; id <= 70; id++) {
            ElectionList one = new ElectionList();
            one.setSubId("c002");
            one.setStudentId(pre + id);
            one.setScore(random.nextDouble() * 10 + 90);
            one.setExperScore(random.nextDouble() * 10 + 87);
            one.setDayScore(random.nextDouble() * 10 + 90);
            list.add(one);
        }
        for (; id <= 106; id++) {
            ElectionList one = new ElectionList();
            one.setSubId("c002");
            one.setStudentId(pre + id);
            one.setScore(random.nextDouble() * 20 + 70);
            one.setExperScore(random.nextDouble() * 20 + 60);
            one.setDayScore(random.nextDouble() * 20 + 76);
            list.add(one);
        }
        for (; id <= 120; id++) {
            ElectionList one = new ElectionList();
            one.setSubId("c002");
            one.setStudentId(pre + id);
            one.setScore(random.nextDouble() * 16 + 54);
            one.setExperScore(random.nextDouble() * 16 + 54);
            one.setDayScore(random.nextDouble() * 16 + 54);
            list.add(one);
        }
        for (; id <= 130; id++) {
            ElectionList one = new ElectionList();
            one.setSubId("c002");
            one.setStudentId(pre + id);
            one.setScore(random.nextDouble() * 14 + 40);
            one.setExperScore(random.nextDouble() * 14 + 40);
            one.setDayScore(random.nextDouble() * 14 + 40);
            list.add(one);
        }

        mapper.insert(list);
    }

    @Override
    public ResultVO normalityTest(String courseId, String classId, String teacherId) {
        List<ElectionList> list = mapper.select(courseId, classId, teacherId);
        System.out.println("数据量：" + list.size());
        double[] scores = getScore(list);

        double[] res = NormalityTest.ksTest(scores);
        double pValue = res[1];
        if (pValue >= NormalityTest.alpha)
            System.out.println("正态");
        else
            System.out.println("非正态");

        ItemVO itemVO = new ItemVO(res[0], res[1], list.size() ,"");
        List<ItemVO> r = new ArrayList<>();
        r.add(itemVO);
        return new ResultVO(res[2], res[3], r);
    }

    @Override
    public void discriminationAnalysis(String courseId, String classId, String teacherId) {
        List<ElectionList> list = mapper.select(courseId, classId, teacherId);
        System.out.println("数据量：" + list.size());
        double[] data = getScore(list);
        double[] doubles = DifferenceAnalysis.calcDiscrimination(data);
        if (doubles[1] < 0.05) {
            System.out.println("区分显著");
        } else {
            System.out.println("区分不显著");
        }
    }

    @Override
    public void courseGradeAnalysis(String courseId, String classId, String teacherId) {
        List<TeacherCourseClass> teacherCourseClasses = mapper.selectClassCount(courseId, classId, teacherId);
        List<double[]> allData = new ArrayList<>();
        int cnt = 0;
        for (TeacherCourseClass one : teacherCourseClasses) {
            List<ElectionList> list = mapper.select(one.getSubId(), one.getClassId(), one.getTeacherId());
            double[] score = getScore(list);
            cnt += score.length;
            allData.add(score);
        }
        System.out.println("数据量：" + cnt);
        double[] doubles = DifferenceAnalysis.courseGradeAnalysis(allData);
        if (doubles[1] < 0.05)
            System.out.println("差异性显著");
        else
            System.out.println("差异性不显著");
    }

    @Override
    public List<Course> getCourses() {
        return mapper.selectCourses();
    }

    @Override
    public List<Teacher> getTeachers(String courseId) {
        return mapper.selectTeacher(courseId);
    }

    private double[] getScore(List<ElectionList> list) {
        int n = list.size();
        double[] scores = new double[n];
        for (int i = 0; i < n; i++) {
            scores[i] = list.get(i).getScore();
        }
        return scores;
    }

    private double[] getExperimentScore(List<ElectionList> list) {
        int n = list.size();
        double[] scores = new double[n];
        for (int i = 0; i < n; i++) {
            scores[i] = list.get(i).getExperScore();
        }
        return scores;
    }
//    void normalityTestByAPI() {
//        List<ElectionList> list = mapper.select();
//        for (ElectionList item : list) {
//            System.out.println(item);
//        }
//        int n = list.size();
//        double[] scores = new double[n];
//        for (int i = 0; i < n; i++) {
//            scores[i] = list.get(i).getScore();
//        }
//
//        double mean = NormalityTest.calcMean(scores);
//        double stdDev = NormalityTest.calcStd(scores, mean);
//        NormalDistribution normalDist = new NormalDistribution(mean, stdDev);
//        KolmogorovSmirnovTest ksTest = new KolmogorovSmirnovTest();
//        double ksStatistic = ksTest.kolmogorovSmirnovStatistic(normalDist, scores);
//        double pValue1 = ksTest.kolmogorovSmirnovTest(normalDist, scores, true);
//        System.out.println("----- API实现 ------");
//        System.out.println("KS统计量: " + ksStatistic);
//        System.out.println("P值: " + pValue1);
//        System.out.println("拒绝原假设（α=0.05）? " + (pValue1 < 0.05));
//    }



}
